﻿using System;
using System.Windows.Forms;

namespace Mjudoku
{
    class SimpleController : Controller
    {
        private View view;
        private Model model;

        public SimpleController(Model model)
        {
            this.model = model;
        }

        public void addView(View view)
        {
            this.view = view;
        }

        public void initGame(int blockSize, int arraySize, Object source)
        {
            model.initArray(blockSize, arraySize);
            //TODO check if source is same size as declared

            model.loadSudoku(source);

            ((Form)view).Show();

            view.prepareFirstDraw(blockSize, arraySize);
        }

        public void quit_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        public void check_Click(object sender, EventArgs e)
        {
            SudokuChecker checker = new SudokuChecker(model.getCisla(), model.getSize(), model.getBlockSize());
            bool correct = checker.checkRows() && checker.checkColumns();
            if (correct)
                MessageBox.Show("Sudoku is correct.");
            else
                MessageBox.Show("Sudoku isn't correct.");
        }

        public void edit_TextChanged(object sender, EventArgs e)
        {
            MjudokuBox box = (MjudokuBox)sender;
            if (System.Text.RegularExpressions.Regex.IsMatch(box.Text, "[^0-9]"))
            {
                MessageBox.Show("Please enter only numbers.");
                box.Text = box.Text.Remove(box.Text.Length - 1);                
                return;
            }            
            if (box.Text != "")
            {
                int row, column, newValue;
                row = box.Row;
                column = box.Column;
                newValue = Convert.ToInt32(box.Text);
                model.editNumber(row, column, newValue);
            }
        }

    }
}
